<template>
    <Modal v-model="perfectCompanyInfoModal" width="650" class="col-2" :closable="false" :mask-closable="false">
        <div slot="header">
            导入随附单据，需要用到IC卡号，请补全以下信息
            <Icon class="f-fr" type="ios-close" @click="cancel"/>
        </div>
        <Form :model="companyForm" ref="companyIcRef" label-position="right" :rules="rules" :label-width="130" inline>
            <Row>
                <iCol span="24">
                    <FormItem label="IC卡号：" prop="icCode" class="icCode">
                        <Input v-model="companyForm.icCode" @on-enter="setAssignFocus('icName')"></Input>
                    </FormItem>
                </iCol>
            </Row>
            <Row>
                <iCol span="24">
                    <FormItem label="卡姓名：" prop="icName">
                        <Input v-model="companyForm.icName" id="ic_name" @on-enter="save"></Input>
                    </FormItem>
                </iCol>
            </Row>
        </Form>
        <div slot="footer">
            <div slot="footer">
                <Button type="primary" @click="save">确定</Button>
                <Button type="default" @click="cancel">取消</Button>
            </div>
        </div>
    </Modal>
</template>

<script>
import { httpPost } from '@/libs/http'

export default {
    name: 'PerfectCompanyInfoModal',
    data () {
        return {
            perfectCompanyInfoModal: false,
            companyForm: {
                icCode: '',
                icName: ''
            },

            rules: {
                icCode: [
                    { required: true, message: '请输入IC卡号', trigger: 'blur' }
                ],
                icName: [
                    { required: true, message: '请输入卡姓名', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        init () {
            this.perfectCompanyInfoModal = true
            this.$nextTick(() => {
                this.$refs['companyIcRef'].resetFields()
            })
        },

        save () {
            this.$refs['companyIcRef'].validate((valid) => {
                if (valid) {
                    httpPost('/customs/sys/saveDecInfo', this.companyForm).then(res => {
                        if (res.code === '0000') {
                            this.$Message.success('保存成功，请继续导入单一窗口')
                            this.cancel()
                        }
                    })
                }
            })
        },

        cancel () {
            this.perfectCompanyInfoModal = false
        },

        // 设置指定的元素获得焦点
        setAssignFocus (className) {
            document.getElementById('ic_name').querySelector('.ivu-input').focus()
        }
    }
}
</script>
